<?php
/*
 * @Author: 乔
 * @Date: 2021-10-08 14:43:03
 * @LastEditTime: 2021-11-05 09:29:12
 * @LastEditors: 乔
 * @FilePath: \tp30\app\admin\controller\Index.php
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use app\common\traits\Jump;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;
use think\facade\Route;
use think\facade\View;

class Index extends BaseController
{
    use Jump;
    public function index()
    {

        //模板输出替换
        //config<view.php
        //'tpl_replace_string'=>[
        //      '__大写__' => '/static'
        //]
        //使用： 在html中进行路径替换

        //模板文件包括
        //{include file='模版文件1,模版文件2,...' /} 引入文件相对于view目录下文件
        //注意：不能a引入b，b在引入a

        return view();
    }

    public function info()
    {
        $where = ['id' => 1];
        $list = \app\admin\model\Program::getProgramList($where)->toArray();
        //$list=Db::name('program')->where('id', 1)->find();
        View::assign('list', $list);
        if (Request::isPost()) {
            $post = Request::post();
            $post['uptime'] = time();
            $post['id'] = 1;
            list($res, $msg) = \app\admin\model\Program::editProgram($post);
            if ($res) {
                $this->success($msg, url('admin/Index/info'));
            } else {
                $this->error($msg);
            }
        }
        return view();
    }
    public function pass()
    {
        $list = session(md5('user'));
        if (Request::isPost()) {
            $post = Request::post();
            echo '<pre>';
            echo print_r($post);
            echo '<pre/>';
            if (md5($post['mpass']) != $list['pwd']) $this->error("原始密码错误 请确认后再输入");
            $arr['password']  = $post['newpass'];
            $arr['code']      = $post['code'];
            $arr['__token__'] = $post['__token__'];
            try {
                validate(\app\admin\validate\Login::class)->scene('qrPwd')->check($arr);
                $where = [
                    'pwd' => md5($post['newpass']),
                ];;
                if (!empty($post['password'])) $where['pwd'] = md5($post['password']);
                $listRes =  \app\admin\model\Admin::where('name', '=', $list['name'])->update($where);
            } catch (ValidateException $e) {
                $this->error($e->getError());
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            if ($listRes) {
                $this->success('密码修改成功');
            }
        }
        View::assign('list', $list);
        return view();
    }
    public function head()
    {
        return view();
    }
}
